package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.AppointmentMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private StatementServiceImpl statementService;
    @Autowired
    private AppointmentMapper service;

    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public void update(StatementItem StatementItem) {
        statementItemMapper.updateByPrimaryKey(StatementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }
    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
//        if (qo.getName()!=null && qo.getName()!="") {
//            qo.setName(qo.getName().substring(0, qo.getName().lastIndexOf(",")));
//        }

        List<StatementItem> statementItems = statementItemMapper.selectForList(qo);
        return new TablePageInfo<StatementItem>(Long.valueOf(statementItems.size()),statementItems);
    }

    @Override
    public List<StatementItem> queryByStatementId(Long statementId) {
        return statementItemMapper.queryByStatementId(statementId);
    }

    @Override
    public void saveList(List<StatementItem> list) {
        StatementItem remove = list.remove(list.size() - 1);
        Long id=remove.getStatementId();
        statementItemMapper.deleteByStatemenId(id);
        BigDecimal discountAmount = remove.getItemPrice();
        Statement statement = statementService.get(id);
       if (Statement.STATUS_PAID.equals(statement.getStatus())){
         throw new BusinessException("已支付的账单不可以再修改");
        }
       if (statement.getAppointmentId()!=null){
           service.cancelOp(statement.getAppointmentId(), Appointment.STATUS_SETTLE);
       }
        BigDecimal totalAmount = new BigDecimal("0.00"); //总消费金额
        BigDecimal totalQuantity = new BigDecimal("0"); // 数量
        for (StatementItem sta : list) {
            statementItemMapper.insert(sta);
           totalAmount=totalAmount.add(sta.getItemPrice().multiply(sta.getItemQuantity()));
           totalQuantity=totalQuantity.add(sta.getItemQuantity());
        }
        statement.setTotalQuantity(totalQuantity);
        statement.setTotalAmount(totalAmount);
        statement.setDiscountAmount(discountAmount);
        statementService.updateStatement(statement);
    }
}
